package com.gyms.manager;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.webkit.JavascriptInterface;

import com.classic.okhttp.AppChangeUtils;
import com.classic.okhttp.Cons;
import com.classic.okhttp.beans.HVUserBaseInfoBean;
import com.classic.okhttp.zmoumall.utils.ToastUtil;
import com.gyms.activity.MyCouponActivityNew;
import com.gyms.activity.WebViewActivity;

import org.greenrobot.eventbus.EventBus;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.concurrent.locks.LockSupport;

import constant.EventConstant;
import utils.AppUtils;
import utils.DeviceUtils;
import utils.StringUtils;
import weight.ToastManger;
import weight.UiEvent;

/**
 * ltx on 2016/12/19 16:30
 * 供js调用的方法
 */
public class AndroidJavaScript {
    private Context mContext;
    private Intent mIntent;

    private  ArrayList<Object> mKeyList;
    private ToastManger mToastManger;

    //加载中等待提示
    public Dialog mLoadingView = null;
    private   JSHandler mHandler;
    private boolean isCommit;//是否是提交
    private Thread mCurThread;
    public static final int S_COMMIT=0;



    public AndroidJavaScript(Context mContext) {
        this.mContext = mContext;
        mKeyList=new ArrayList<>();
        mToastManger=ToastManger.getInstance(mContext);
        mHandler=new JSHandler(this);
    }

    /**
     * 解决内存泄漏
     */

    private static class JSHandler extends Handler{
        private  WeakReference<AndroidJavaScript> mJS;
        private JSHandler(AndroidJavaScript js){
            mJS =new WeakReference<>(js);
        }
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            AndroidJavaScript js=mJS.get();
            if(null==mJS){
                return;
            }
            switch (msg.what){
                case S_COMMIT:
                    js.isCommit= (boolean) msg.obj;
                    LockSupport.unpark(js.mCurThread);//唤醒子线程
                    break;
            }
        }
    }

    /**
     * 获取设备类型
     * @return 设备类型
     */
    @JavascriptInterface
    public String getDeviceInfo() {
        String mDeviseId= DeviceUtils.getInstance(mContext).getID();//设备id
        String mVersionName= AppUtils.getAppVersionName(mContext);//版本名称
        StringBuffer sb=new StringBuffer("Android").append(";").append(mDeviseId).append(";").append(mVersionName);
        ToastUtil.showToast(mContext,"获取设备类型");
       return sb.toString();
    }

    /**
     * 弹出登录界面
     */
    @JavascriptInterface
    public void showLogin() {
        mKeyList.clear();
        mKeyList.add(EventConstant.FLAG_GO_LOGIN);
        if(isLogin()){
            return;
        }
        EventBus.getDefault().post(new UiEvent(EventConstant.S_WEBVIEW_START_ACTIVITY,mKeyList));
    }

    /**
     * 判断登录状态
     */
    @JavascriptInterface
    public boolean isLogin() {
        return DataCenter.getInstance().checkIsLogin();
    }

    /**
     * 返回昵称
    */
    @JavascriptInterface
    public String getNickName() {
        HVUserBaseInfoBean mHVUserBaseInfoBean=DataCenter.getInstance().getUser();
        String name="";
        if (!StringUtils.isNull(mHVUserBaseInfoBean)) {
            name = mHVUserBaseInfoBean.getUserName();
        }
        return name;
    }

    /**
     * 前往场馆详情
     */
    @JavascriptInterface
    public void showVenue(String mGymId) {
        mKeyList.clear();
        mKeyList.add(EventConstant.FLAG_GO_GYM_DETAIL);
        mKeyList.add(mGymId);
        EventBus.getDefault().post(new UiEvent(EventConstant.S_WEBVIEW_START_ACTIVITY,mKeyList));

    }

    /**
     * 前往课程详情
     */
    @JavascriptInterface
    public void showCourse(String mCourseId) {
        mKeyList.clear();
        mKeyList.add(EventConstant.FLAG_GO_COURSE_DETAIL);
        mKeyList.add(mCourseId);
        EventBus.getDefault().post(new UiEvent(EventConstant.S_WEBVIEW_START_ACTIVITY,mKeyList));
    }

    /**
     * 前往我的订单
     */
    @JavascriptInterface
    public boolean showMyOrders() {
        boolean isLogin=isLogin();
        if(isLogin){
            mKeyList.clear();
            mKeyList.add(EventConstant.FLAG_GO_MY_ORDER);
            EventBus.getDefault().post(new UiEvent(EventConstant.S_WEBVIEW_START_ACTIVITY,mKeyList));
        }
        return isLogin;
    }

    /**
     * 前往我的卡券
     */
    @JavascriptInterface
    public boolean showMyCoupons() {
        boolean isLogin=isLogin();
        if(isLogin){
            mKeyList.clear();
            mKeyList.add(EventConstant.FLAG_GO_MY_CARDACTIVITY);
            EventBus.getDefault().post(new UiEvent(EventConstant.S_WEBVIEW_START_ACTIVITY,mKeyList));
            mIntent=new Intent(mContext, MyCouponActivityNew.class);
        }
        return isLogin;
    }

    /**
     * 显示分享链接按钮
     */
    @JavascriptInterface
    public boolean showShareButton(String title,String content,String url) {
        boolean mIsCanShare;
        mKeyList.clear();
        mKeyList.add(EventConstant.FLAG_SHOW_SHARE_DIALOG);
        mKeyList.add(title);
        mKeyList.add(content);
        mKeyList.add(url);
        EventBus.getDefault().post(new UiEvent(EventConstant.S_WEBVIEW_START_ACTIVITY,mKeyList));
//        doShowShare(title,content,url);
        //呼啦场馆
        mIsCanShare = AppChangeUtils.isGymApp;
        return mIsCanShare;
    }

    /**
     * 隐藏分享按钮
     */
    @JavascriptInterface
    public void hideShareButton() {
        mKeyList.clear();
        mKeyList.add(EventConstant.FLAG_DISMISS_SHARE_DIALOG);
        EventBus.getDefault().post(new UiEvent(EventConstant.S_WEBVIEW_START_ACTIVITY,mKeyList));
    }
    /**
     * 获取token
     */
    @JavascriptInterface
    public String getUserToken() {
        String mToken=DataCenter.getInstance().checkIsLogin()?DataCenter.getInstance().getToken():"";
        return mToken;
    }

    /**
     *
     * @param gymId 场馆Id
     * @param projectId 项目Id
     * @param time 时间
     */
    @JavascriptInterface
    public void showVenueBooking(String gymId,String projectId,int time){
        mKeyList.clear();
        mKeyList.add(EventConstant.FLAG_GO_CHOSESEAT);
        mKeyList.add(gymId);
        mKeyList.add(projectId);
        mKeyList.add(time);
        EventBus.getDefault().post(new UiEvent(EventConstant.S_WEBVIEW_START_ACTIVITY,mKeyList));
    }

    @JavascriptInterface
    public void getTodayStepCount(String m){
        EventBus.getDefault().post(new UiEvent("js",m));
    }

    /**
     * 设置标题
     * @param title 标题名称
     */
    @JavascriptInterface
    public void setTitle(String title){
        EventBus.getDefault().post(new UiEvent(EventConstant.S_WEBVIEW_SETTITLE,title));
    }


    /**
     * 关掉页面
     */
    @JavascriptInterface
    public void close(){
        EventBus.getDefault().post(new UiEvent(EventConstant.S_WEBVIEW_CLONSE,null));
    }

    /**
     * 跳转到网页
     * @param url
     */
    public void showWebView(String url,String title){
        mIntent = new Intent(mContext, WebViewActivity.class);
        mIntent.putExtra("title", title);
        mIntent.putExtra("url", url);
        mContext.startActivity(mIntent);
    }

    /**
     * 跳转系统浏览器
     * @param url
     */
    public void showSystemWeb(String url){
        Uri uri = Uri.parse(url);
        mIntent = new Intent(Intent.ACTION_VIEW, uri);
        mContext.startActivity(mIntent);
    }
    @JavascriptInterface
    public String getUserId(){
        return StringUtils.isNull(DataCenter.getInstance().getUser())?null:DataCenter.getInstance().getUser().getUserid();
    }

    /**
     * js 登录回调
     * @param mFunction 方法名
     */

    @JavascriptInterface
    public void checkLogin(String mFunction){
        if (isLogin()){//已经登录
            EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_LOGIN_ED,mFunction));
        }else {
            EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_UN_LOGIN,mFunction));
        }
    }

    /**
     * js调用本地 保存今日步数
     */

    @JavascriptInterface
    public void saveTodayStepCount(String mFunction){
        EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_SAVE_TODAY_STEP_COUNT,mFunction));
    }

    /**
     * 返回版本号参数
     */
    @JavascriptInterface
    public String getVersionCode(){
        return Cons.mVersionCode;
    }

    @JavascriptInterface
    public void showSuccessHUD(String mMessage){
        mToastManger.showToast(ToastManger.S_TOAST_SUCCESS,mMessage);
    }

    @JavascriptInterface
    public void showErrorHUD(String mMessage){
        mToastManger.showToast(ToastManger.S_TOAST_FAIL,mMessage);
    }

    @JavascriptInterface
    public void showTextHUD(String mMessage){
        mToastManger.showToast(ToastManger.S_TOAST_NONE,mMessage);
    }

    @JavascriptInterface
    public void showLoadingHUD(){
        EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_SHOW_LOADING_HUD,null));
    }

    @JavascriptInterface
    public void hideHUD(){
        EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_HIDE_HUD,null));
    }

    @JavascriptInterface
    public void showAlert(String mMessage){
        mCurThread=Thread.currentThread();
        if(!StringUtils.isNull(mKeyList)){
            mKeyList.clear();
        }
        mKeyList.add(mMessage);
        mKeyList.add(mCurThread);
        EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_SHOW_ALERT,mKeyList));
        LockSupport.park();
    }

    @JavascriptInterface
    public boolean showConfirm(String mMessage){
        mCurThread=Thread.currentThread();
        if(!StringUtils.isNull(mKeyList)){
            mKeyList.clear();
        }
        mKeyList.add(mMessage);
        mKeyList.add(mHandler);
        EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_SHOW_CONFIRM,mKeyList));
        LockSupport.park();

        return isCommit;
    }

    //弹出分享框或者直接分享
    @JavascriptInterface
    public void showShareView(String title,String content,String url,int shareType,String callBackFunStr){
        if(!StringUtils.isNull(mKeyList)){
            mKeyList.clear();
        }
        mKeyList.add(title);
        mKeyList.add(content);
        mKeyList.add(url);
        mKeyList.add(shareType);
        mKeyList.add(callBackFunStr);
        EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_DO_SHOW_SHARE_DIALOG,mKeyList));
    }

    /**
     * 拨打电话
     * @param mPhoneNum 电话号码
     */
    @JavascriptInterface
    public void callMobilePhone(String mPhoneNum){
        EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_PHONE_NUM,mPhoneNum));
    }
    /**
     * 弹出升级框
     */
    @JavascriptInterface
    public void tryUpgrade(){
        EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_TRY_UPDATE,null));
    }


}
